// pages/student/transferLessons/transferLessons.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],//初始化获取的下拉框数据
    show_arr: [],//显示的下拉框数据
    index: 0,//初始化显示的数据
    children_id: '',//需要上传的学员的id
    sum: '',//控制class
    course: [],//选择课程的切换数据
    time_date: [],//选择课程的切换数据
    member_id:'',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var This = this;
    This.setData({
        member_id: options.member_id
    })
    This.loadFormate();
  },
  loadFormate:function(){
    var This = this; 
    app._get('coach/student/memberType'
      , {
        phone: wx.getStorageSync('phone'),
        openid: wx.getStorageSync('openid'),
        store_id: wx.getStorageSync('store_id'),
        member_id: This.data.member_id
      }, function (res) {
        if (res.code == 200) {
          This.setData({
            course: res.itemType,
            array: res.coach_list,
            children_id: res.coach_list[0].id
          })
          var name = This.data.array;
          var arrList = [];
          for (var index in name) { 
              arrList.push(name[index].coach_name + '  ' + name[index].contact)
          }
          This.setData({
            show_arr: arrList
          })
        } else {
          wx.showToast({
            title: res.error,
            icon: 'none',
            duration: 2000
          })
        }
      })      
  },
  //课程切换按钮
  active_cl: function (e) {
    var This = this
    This.setData({
      sum: e.target.id,
    })
  },
  //选择教练的select数据
  bindPickerChange: function (e) {
    var This = this;
    This.setData({
      index: e.detail.value,
    })

    for (var index in This.data.array) {
      if (index == e.detail.value) {
        This.setData({
          children_id: This.data.array[index].id
        })
      }
    }
  },
  //提交数据
  add_to: function () {
    var This = this;
    if (This.data.sum !=''){
      app._post_form('coach/course/shiftCourse'
        , {
            member_id: This.data.member_id,//学员id
            coach_id: This.data.children_id,//教练id
            item_id: This.data.sum,//课程id
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
        }, function (res) {
          if (res.status == 1) {
            setTimeout(function () {
              wx.showToast({
                title: '转课成功',
                icon: 'none',
                duration: 2000
              });
              wx.switchTab({
                  url: '/pages/passenger/passenger',
              })
              setTimeout(function () {
                wx.switchTab({
                  url: "../student"
                })
              }, 2000)
            }, 0)
          } else {
            setTimeout(function(){
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            },0)
          }
        })
    }else{
      wx.showToast({
        title: '请选择课程类型',
        icon: 'none',
        duration: 2000
      });
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})